import { defineStore } from 'pinia'
import { ref } from 'vue'
import { roleApi } from '@/api'

export const useRoleStore = defineStore('role', () => {
  const roleOptions = ref<any[]>([])

  const fetchRoleOptions = async () => {
    // 如果 roleOptions 不為空數組，則直接返回
    if (roleOptions.value.length > 0) {
      return roleOptions.value
    }
    try {
      const { data } = await roleApi.getRoleOptions()
      roleOptions.value = data
      return roleOptions.value
    } catch (error) {
      console.error('獲取角色列表失敗:', error)
    }
  }

  return {
    roleOptions,
    fetchRoleOptions,
  }
})
